import { updatePermission } from '@/services/api/api-admin-permission';
import {
  ProForm,
  ProFormDigit,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { DrawerForm } from '@ant-design/pro-form';
import { Button, message } from 'antd';
import React from 'react';

interface EditPermissionProps {
  data: AdminPermissionData.Permission;
  onRefurbish: () => void;
}
const EditPermission: React.FC<EditPermissionProps> = ({ data, onRefurbish }) => {
  const selectAccesssType = [
    {
      label: '页面',
      value: 1,
    },
    {
      label: '模块',
      value: 2,
    },
    {
      label: '按钮',
      value: 3,
    },
    {
      label: '方法',
      value: 4,
    },
  ];

  return (
    <DrawerForm
      key="add_role"
      title="修改"
      initialValues={{
        parentName: data.parentName,
        type: data.type,
        seq: data.seq,
        name: data.name,
        value: data.value,
        describes: data.describes,
      }}
      drawerProps={{
        destroyOnClose: true,
      }}
      trigger={<Button type="primary">修改</Button>}
      onVisibleChange={(visible: boolean) => {
        if (visible) {
        }
      }}
      onFinish={async (values) => {
        const response = await updatePermission({
          ...values,
          id: data.id,
          parentId: data.parentId,
        });
        if (response.code === 0) {
          message.success('提交成功');
          onRefurbish();
        }
        return true;
      }}
    >
      <ProForm.Group>
        <ProFormText width="md" name="parentName" label="父级权限" disabled />
        <ProFormSelect
          width="md"
          options={selectAccesssType}
          name="type"
          label="类型"
          rules={[{ required: true, message: '请选择类型' }]}
        />
        <ProFormDigit width="md" label="排序" name="seq" min={1} max={250} />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText
          width="md"
          name="name"
          label="名称"
          rules={[{ required: true, message: '请输入名称' }]}
        />
        <ProFormText
          width="md"
          name="value"
          label="代码"
          rules={[{ required: true, message: '请输入代码' }]}
        />
        <ProFormTextArea width="xl" name="describes" label="描述" />
      </ProForm.Group>
    </DrawerForm>
  );
};
export default EditPermission;
